/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uloha4.view;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import javax.swing.JComponent;

/**
 *
 * @author Tomique
 */
public class ShapeFactory {

    public static void paintTestObjects(Graphics2D g) {
        g.setColor(Color.BLACK);
        int offset = 100;
        g.fillOval(-offset, -offset, 50, 50);
        g.fillOval(-offset, offset, 50, 50);
        g.fillOval(offset, -offset, 50, 50);
        g.fillOval(offset, offset, 50, 50);
        drawArrow(g, 0, 0, 100, 100, 10, 0, true);
    }

    /**
     * Draws arrow to a specified canvas
     *
     * @param g Graphic container, where to paint the arrow
     * @param x0 X coordinate of the start point of the line
     * @param y0 Y coordinate of the start point of the line
     * @param x1 X coordinate of the end point of the line
     * @param y1 Y coordinate of the end point of the line
     * @param arrowSize Size of the the arrow itself
     * @param enable Flag, whether the arrow will be displayed
     */
    public static void drawArrow(Graphics2D g, int x0, int y0, int x1, int y1, int arrowSize, float nodeSize, boolean enable) {
        // ulozim vychozi transformaci
        AffineTransform old = g.getTransform();

        final int OFFSET = 3;

        // rozdil v x-ovych souradnicich koncovych bodu
        double dx = x1 - x0;
        // rozdil v y-ovych souradnicich koncovych bodu
        double dy = y1 - y0;
        // slope dane usecky
        double angle = Math.atan2(dy, dx);
        // zaokrouhlena delka dane usecky
        int norm = (int) Math.round(Math.sqrt(dx * dx + dy * dy));

        // definuji transformaci pro nove vznikle tvary      
        AffineTransform at = AffineTransform.getTranslateInstance(x0, y0);
        at.concatenate(AffineTransform.getRotateInstance(angle));
        g.transform(at);


        // vykreslim sipku ve vychozim stavu a definovana transformace se postara o jeji spravne napozicovani
        if (enable) {
            g.drawLine(0, 0, norm - arrowSize, 0);
            norm -= arrowSize;
            int[] xCoord = {norm + OFFSET, norm - arrowSize, norm - arrowSize + OFFSET, norm - arrowSize, norm + OFFSET};
            int[] yCoord = {0, -arrowSize / 2, 0, arrowSize / 2, 0};
            g.fillPolygon(xCoord, yCoord, xCoord.length);
        } else {
            g.drawLine(0, 0, (int)((float)norm - (nodeSize / 2.0f)), 0);
        }

        // obnovim puvodni transformaci
        g.setTransform(old);
    }

    public static void paintGradient(JComponent comp, Graphics2D g) {
        GradientPaint gp = new GradientPaint(0, 0, comp.getBackground().brighter().brighter(), 0, comp.getHeight(), comp.getBackground().darker().darker());
        g.setPaint(gp);
        g.fillRect(0, 0, comp.getWidth(), comp.getHeight());
    }
}
